/*
 * @Descripttion: 描述
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-04-27 17:31:22
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-04 13:31:43
 */

const DB = require('../../lib/DB')
const { Success, Fail } = require('../../lib/Response');
module.exports = async function (ctx, next) {
    const { name, pwd } = ctx.request.body;

    const res = await DB.models.user.findOne({ where: { name, pwd } })
    if (!!res) {
        // 更新登录时间
        res.login_time = Date.now();
        await res.save();
        ctx.session.user = res.toJSON();
        ctx.body = new Success(ctx.session.user)
        await next();
    } else {
        ctx.body = new Fail('用户不存在或密码错误')
    }
}